package com.contory.ssn_lgnd.framework.gl;

import android.util.Log;

public class Font {
    public final int glyphWidth;
    public final int glyphHeight;
    public final TextureRegion[] glyphs = new TextureRegion[96];   
    
    public Font(int textureWidth, int textureHeight, 
                int offsetX, int offsetY,
                int glyphsPerRow, int glyphWidth, int glyphHeight) {     
        this.glyphWidth = glyphWidth;
        this.glyphHeight = glyphHeight;
        int x = offsetX;
        int y = offsetY;
        for(int i = 0; i < 96; i++) {
            glyphs[i] = new TextureRegion(textureWidth, textureHeight, x, y, glyphWidth, glyphHeight);
            x += glyphWidth;
            if(x == offsetX + glyphsPerRow * glyphWidth) {
                x = offsetX;
                y += glyphHeight;
            }
        }        
    }
    
    public void drawText(SpriteBatcher batcher, String text, float x, float y, float wd, float ht, float r, float g, float b, float a) {
        int len = text.length();
        int letter_num = text.replaceAll("\\p{Space}", "").length();
        int space_num = len - letter_num;
        x = x - (letter_num * wd / 2) - (space_num * wd / 4) + wd / 2;
        for(int i = 0; i < len; i++) {
            int c = text.charAt(i) - ' ';
            if(c < 0 || c > glyphs.length - 1) 
                continue;
            
            float tmp_wd = wd;
            if(c == 0)
            	tmp_wd = wd / 2;
            TextureRegion glyph = glyphs[c];
            //Log.d("1", String.valueOf(tmp_wd));
            //batcher.drawSprite(x, y, tmp_wd, ht, glyph);
            batcher.drawSprite(x, y, tmp_wd, ht, r, g, b, a, glyph);
            x += tmp_wd;
        }
    }
}
